use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(User::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(User::Id)
                            .big_integer()
                            .primary_key()
                            .auto_increment()
                            .comment("ID"),
                    )
                    .col(
                        ColumnDef::new(User::EditorType)
                            .string_len(10)
                            .not_null()
                            .default("system")
                            .comment("编辑类型"),
                    )
                    .col(
                        ColumnDef::new(User::EditorId)
                            .big_integer()
                            .not_null()
                            .default(0)
                            .comment("编辑ID"),
                    )
                    .col(
                        ColumnDef::new(User::No)
                            .string_len(15)
                            .not_null()
                            .default("")
                            .comment("用户编号"),
                    )
                    .col(
                        ColumnDef::new(User::Name)
                            .string_len(30)
                            .not_null()
                            .default("")
                            .comment("Name"),
                    )
                    .col(
                        ColumnDef::new(User::Realname)
                            .string_len(30)
                            .not_null()
                            .default("")
                            .comment("真实姓名"),
                    )
                    .col(
                        ColumnDef::new(User::Nickname)
                            .string_len(30)
                            .not_null()
                            .default("")
                            .comment("昵称"),
                    )
                    .col(
                        ColumnDef::new(User::UserType)
                            .string_len(50)
                            .not_null()
                            .default("member")
                            .comment("用户类型"),
                    )
                    .col(
                        ColumnDef::new(User::Gender)
                            .tiny_integer()
                            .not_null()
                            .default(0)
                            .comment("性别"),
                    )
                    .col(
                        ColumnDef::new(User::Phone)
                            .string_len(11)
                            .not_null()
                            .default("")
                            .comment("手机号码"),
                    )
                    .col(
                        ColumnDef::new(User::AvatarPath)
                            .string_len(150)
                            .not_null()
                            .default("")
                            .comment("用户头像"),
                    )
                    .col(
                        ColumnDef::new(User::Email)
                            .string_len(50)
                            .not_null()
                            .default("")
                            .comment("邮箱"),
                    )
                    .col(
                        ColumnDef::new(User::DataSourceId)
                            .big_integer()
                            .not_null()
                            .default(0)
                            .comment("注册来源"),
                    )
                    .col(
                        ColumnDef::new(User::Password)
                            .string_len(64)
                            .not_null()
                            .default("")
                            .comment("登录密码"),
                    )
                    .col(
                        ColumnDef::new(User::OldPassword)
                            .string_len(64)
                            .not_null()
                            .default("")
                            .comment("原密码"),
                    )
                    .col(
                        ColumnDef::new(User::Salt)
                            .string_len(5)
                            .not_null()
                            .default("")
                            .comment("Salt"),
                    )
                    .col(
                        ColumnDef::new(User::PasswordModifiedAt)
                            .date_time()
                            .comment("密码修改时间"),
                    )
                    .col(
                        ColumnDef::new(User::AttemptedTimes)
                            .tiny_integer()
                            .not_null()
                            .default(0)
                            .comment("登录失败次数"),
                    )
                    .col(
                        ColumnDef::new(User::LastAttemptedAt)
                            .date_time()
                            .comment("最后登录失败时间"),
                    )
                    .col(
                        ColumnDef::new(User::LastLoginId)
                            .big_integer()
                            .not_null()
                            .default(0)
                            .comment("最近一次登录ID"),
                    )
                    .col(
                        ColumnDef::new(User::LastLoginAt)
                            .date_time()
                            .comment("最近一次登录时间"),
                    )
                    .col(
                        ColumnDef::new(User::IsAuthed)
                            .boolean()
                            .not_null()
                            .default(false)
                            .comment("是否已认证"),
                    )
                    .col(
                        ColumnDef::new(User::IsEnabled)
                            .boolean()
                            .not_null()
                            .default(true)
                            .comment("是否启用"),
                    )
                    .col(
                        ColumnDef::new(User::IsTest)
                            .boolean()
                            .not_null()
                            .default(false)
                            .comment("是否测试用户"),
                    )
                    .col(
                        ColumnDef::new(User::IsDeleted)
                            .boolean()
                            .not_null()
                            .default(false)
                            .comment("是否删除"),
                    )
                    .col(
                        ColumnDef::new(User::CreatedAt)
                            .date_time()
                            .not_null()
                            .comment("创建时间"),
                    )
                    .col(
                        ColumnDef::new(User::UpdatedAt)
                            .date_time()
                            .not_null()
                            .comment("更新时间"),
                    )
                    .col(
                        ColumnDef::new(User::DeletedAt)
                            .date_time()
                            .comment("删除时间"),
                    )
                    .to_owned(),
            )
            .await?;
        manager
            .create_index(
                Index::create()
                    .name("idx-by-email")
                    .table(User::Table)
                    .col(User::Email)
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(User::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum User {
    Table,
    Id,
    EditorType,
    EditorId,
    No,
    Name,
    Realname,
    Nickname,
    UserType,
    Gender,
    Phone,
    AvatarPath,
    Email,
    DataSourceId,
    Password,
    OldPassword,
    Salt,
    PasswordModifiedAt,
    AttemptedTimes,
    LastAttemptedAt,
    LastLoginId,
    LastLoginAt,
    IsAuthed,
    IsEnabled,
    IsTest,
    IsDeleted,
    DeletedAt,
    CreatedAt,
    UpdatedAt,
}
